package com.qsmx.store.app.util;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.appcompat.widget.AppCompatTextView;

import com.qsmx.store.app.R;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.util.dimen.DimenUtil;

/**
 * 文 件 名: DialogUtil.java
 * 描 述: 对话框工具类
 * 创 建 人: dingyu
 * 创建时间: 2015-10-23
 */
@SuppressLint({"NewApi", "InflateParams"})
public class DialogUtil {

    private static Dialog errorDialog;
    private static Dialog twoButtonDialog;
    private static Dialog noButtonDialog;

    private static AppCompatTextView tvErrorInfo;
    private static AppCompatTextView tvRetry;

    /**
     * 显示对话框
     */
    public static void showDialog(Dialog dialog) {
        if (null != dialog && !dialog.isShowing()) {
            dialog.show();
        }
    }

    /**
     * 关闭对话框
     */
    public static void closeDialog(Dialog dialog) {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public static void showErrorDialog(AtmosDelegate mDelegate, Context mContext, String errInfo, String bottomText, final View.OnClickListener listener) {
        if (errorDialog != null && errorDialog.isShowing()) {
            errorDialog.dismiss();
        }
        try {
            if (mDelegate.isAdded()) {
                errorDialog = new Dialog(mContext, R.style.dialog);
                errorDialog.setCancelable(false);
                Window window = errorDialog.getWindow();
                window.setContentView(R.layout.dialog_error_tips);
                int width = (int) (DimenUtil.getScreenWidth() * 0.8);
                window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

                tvErrorInfo = window.findViewById(R.id.tv_error_info);
                tvRetry = window.findViewById(R.id.tv_retry);

                tvErrorInfo.setText(errInfo);
                tvRetry.setText(bottomText);
                tvRetry.setOnClickListener(listener);

                errorDialog.show();
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public static void showErrorDialog(AtmosDelegate mDelegate, Context mContext, String
            errInfo, String bottomText) {
        if (errorDialog != null && errorDialog.isShowing()) {
            errorDialog.dismiss();
        }
        try {
            if (mDelegate.isAdded()) {
                errorDialog = new Dialog(mContext, R.style.dialog);
                Window window = errorDialog.getWindow();
                window.setContentView(R.layout.dialog_error_tips);
                int width = (int) (DimenUtil.getScreenWidth() * 0.6);
                window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

                AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
                AppCompatTextView tvRetry = window.findViewById(R.id.tv_retry);

                tvErrorInfo.setText(errInfo);
                tvRetry.setText(bottomText);
                tvRetry.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (ClickUtil.isNotFastClick()) {
                            errorDialog.dismiss();
                        }
                    }
                });

                errorDialog.show();
            }
        } catch (Exception e) {

        }
    }

    public static void showErrorDialog(Context mContext, String errInfo, String bottomText) {
        if (errorDialog != null && errorDialog.isShowing()) {
            errorDialog.dismiss();
        }
        try {
            errorDialog = new Dialog(mContext, R.style.dialog);
            Window window = errorDialog.getWindow();
            window.setContentView(R.layout.dialog_error_tips);
            int width = (int) (DimenUtil.getScreenWidth() * 0.8);
            window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

            AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
            AppCompatTextView tvRetry = window.findViewById(R.id.tv_retry);

            tvErrorInfo.setText(errInfo);
            tvRetry.setText(bottomText);
            tvRetry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    errorDialog.dismiss();
                }
            });
            errorDialog.show();
        } catch (Exception e) {

        }
    }

    public static void showNotCancelErrorDialog(Context mContext, String errInfo, String bottomText) {
        if (errorDialog != null && errorDialog.isShowing()) {
            errorDialog.dismiss();
        }
        try {
            errorDialog = new Dialog(mContext, R.style.dialog);
            errorDialog.setCancelable(false);
            Window window = errorDialog.getWindow();
            window.setContentView(R.layout.dialog_error_tips);
            int width = (int) (DimenUtil.getScreenWidth() * 0.5);
            window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

            AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
            AppCompatTextView tvRetry = window.findViewById(R.id.tv_retry);

            tvErrorInfo.setText(errInfo);
            tvRetry.setText(bottomText);
            tvRetry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    errorDialog.dismiss();
                }
            });
            errorDialog.show();
        } catch (Exception e) {

        }
    }

    public static void showTwoButtonDialog(Context mContext, String tipsInfo, String bottomLeftText, String bottomRightText, View.OnClickListener leftClickListener, View.OnClickListener rightClickListener) {
        if (twoButtonDialog != null && twoButtonDialog.isShowing()) {
            twoButtonDialog.dismiss();
        }
        try {
            twoButtonDialog = new Dialog(mContext, R.style.dialog);
            Window window = twoButtonDialog.getWindow();
            window.setContentView(R.layout.dialog_two_btn_tips);
            int width = (int) (DimenUtil.getScreenWidth() * 0.6);
            window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

            AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
            AppCompatTextView tvLeftTips = window.findViewById(R.id.tv_left_tips);
            AppCompatTextView tvRightTips = window.findViewById(R.id.tv_right_tips);

            tvErrorInfo.setText(tipsInfo);
            tvLeftTips.setText(bottomLeftText);
            tvRightTips.setText(bottomRightText);

            tvLeftTips.setOnClickListener(leftClickListener);
            tvRightTips.setOnClickListener(rightClickListener);

            twoButtonDialog.show();
        } catch (Exception e) {

        }
    }

    public static void showNoButtonDialog(Context mContext, String tipsInfo, DialogInterface.OnCancelListener listener) {
        if (noButtonDialog != null && noButtonDialog.isShowing()) {
            noButtonDialog.dismiss();
        }
        try {
            noButtonDialog = new Dialog(mContext, R.style.dialog);
            Window window = noButtonDialog.getWindow();
            window.setContentView(R.layout.dialog_two_btn_tips);
            int width = (int) (DimenUtil.getScreenWidth() * 0.6);
            window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

            AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
            AppCompatTextView tvLeftTips = window.findViewById(R.id.tv_left_tips);
            AppCompatTextView tvRightTips = window.findViewById(R.id.tv_right_tips);

//            tvErrorInfo.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View view) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString(FusionString.QR_CODE, "111111");
//                    EventBus.getDefault().removeAllStickyEvents();
//                    EventBus.getDefault().postSticky(new QrScanEvent(bundle));
//                    return true;
//                }
//            });

            tvLeftTips.setVisibility(View.GONE);
            tvRightTips.setVisibility(View.GONE);

            tvErrorInfo.setText(tipsInfo);

            noButtonDialog.show();

            noButtonDialog.setOnCancelListener(listener);
        } catch (Exception e) {

        }
    }

    public static void showNoButtonDialog(Context mContext, String tipsInfo) {
        if (noButtonDialog != null && noButtonDialog.isShowing()) {
            noButtonDialog.dismiss();
        }
        try {
            noButtonDialog = new Dialog(mContext, R.style.dialog);
            noButtonDialog.setCancelable(false);
            Window window = noButtonDialog.getWindow();
            window.setContentView(R.layout.dialog_two_btn_tips);
            int width = (int) (DimenUtil.getScreenWidth() * 0.6);
            window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

            AppCompatTextView tvErrorInfo = window.findViewById(R.id.tv_error_info);
            AppCompatTextView tvLeftTips = window.findViewById(R.id.tv_left_tips);
            AppCompatTextView tvRightTips = window.findViewById(R.id.tv_right_tips);

//            tvErrorInfo.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View view) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString(FusionString.QR_CODE, "111111");
//                    EventBus.getDefault().removeAllStickyEvents();
//                    EventBus.getDefault().postSticky(new QrScanEvent(bundle));
//                    return true;
//                }
//            });

            tvLeftTips.setVisibility(View.GONE);
            tvRightTips.setVisibility(View.GONE);

            tvErrorInfo.setText(tipsInfo);

            noButtonDialog.show();
        } catch (Exception e) {

        }
    }

//    public static void showLoadingDialog(final Context mContext, String message) {
//        loadingDialog = new Dialog(mContext, R.style.dialog);
//        loadingDialog.setCancelable(false);
//        Window window = loadingDialog.getWindow();
//        window.setContentView(R.layout.dialog_loading);
//        int width = (int) (DimenUtil.getScreenWidth() * 0.8);
//        window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);
//
//        TextView tv = window.findViewById(R.id.loading_tv);
//        AppCompatImageView ivLoading = window.findViewById(R.id.iv_loading);
//
//        Animation rotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate_anim);
//        if (rotate != null) {
//            ivLoading.startAnimation(rotate);
//        } else {
//            ivLoading.setAnimation(rotate);
//            ivLoading.startAnimation(rotate);
//        }
//
//        tv.setText(message);
//
//        loadingDialog.show();
//    }

    public static void dismissErrorDialog() {
        if (errorDialog != null) {
            errorDialog.dismiss();
        }
    }

    public static void dismissTwoBtnDialog() {
        if (twoButtonDialog != null) {
            twoButtonDialog.dismiss();
        }
    }

    public static void dismissNoBtnDialog() {
        if (noButtonDialog != null && noButtonDialog.isShowing()) {
            noButtonDialog.dismiss();
        }
    }

    public static Dialog getNoButtonDialog() {
        return noButtonDialog;
    }

    private static String getHtmlData(String bodyHTML) {
        String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:100%; height:auto;}*{margin:0px;}</style>"
                + "</head>";
        return "<html>" + head + "<body style=\"background-color: #00000000;\">" + bodyHTML + "</body></html>";
    }
}
